package errorx

import (
	"fmt"
)

// BizError 业务错误
type BizError struct {
	code int
	msg  string
}

var (
	AccessTokenInvalid BizError = NewBizCode(301, "access token invalid")
	PermissionErr      BizError = NewBizCode(401, "token error")
	BizErr             BizError = NewBizCode(500, "biz error")
	EsIndexErr         BizError = NewBizCode(200001, "索引名不能为空")
	EsIndexAliasErr    BizError = NewBizCode(200002, "别名不能为空")
)

// Error 错误消息
func (e BizError) Error() string {
	return e.msg
}

// Code 错误码
func (e BizError) Code() int {
	return e.code
}

func (e BizError) String() string {
	return fmt.Sprintf("errCode: %d, errMsg: %s", e.Code(), e.Error())
}

// NewBiz 创建业务逻辑错误结构体，默认为业务逻辑错误
func NewBiz(msg string, formats ...any) BizError {
	return BizError{code: BizErr.code, msg: fmt.Sprintf(msg, formats...)}
}

// NewBizCode 创建业务逻辑错误结构体，可设置指定错误code
func NewBizCode(code int, msg string, formats ...any) BizError {
	return BizError{code: code, msg: fmt.Sprintf(msg, formats...)}
}

